<style>
.layui-table-cell {
/*    padding:0 5px;
    height:auto;*/
/*    overflow:visible;*/
/*    text-overflow:inherit;*/
/*    white-space:normal;	           
    word-break: break-all;*/
}
.blue{cursor: pointer;}
</style>
<div style="padding:50px 0px 0px 90px;">
	<div>
    	<div>同步结果</div>
        <div class="importResult">
        
        </div>
	同步历史订单时间段：<input type="text" name="create_date_range" readonly="" id="create_date_range" lay-verify="" placeholder="选择下单日期" style="height:26px;width:300px;">
    
     <a href="javascript:startImport()" data-url="{:url('sync')}" title="同步指定日期订单" class="layui-btn layui-btn-primary layui-btn-sm sync-date"><i class="layui-icon">&#xe669;</i>开始同步订单</a>   <a href="javascript:stopImport()" class="layui-btn layui-btn-primary layui-btn-sm">停止同步</a><br />
	当前已同步到日期：<input type="text" name="current_import_date" class="current_import_date" lay-verify="" placeholder="选择已同步日期" style="height:26px;width:300px;">
	
</div>
<hr />
<div id="syncDateTpl" style="width:460px;">
	<form class="layui-form layui-form-pane ml10 mt10 sync-date-form" action="{:url()}" method="get">
		<div class="layui-inline">
			<label class="layui-form-label" style="padding:8px 3px;">正在同步时间</label>
			<div class="layui-input-inline">
				<input type="text" name="sync_date_range" id="sync_date_range"  placeholder="选择同步时间" class="layui-input" style="width:300px;">
			</div>
		</div>
		<div class="layui-inline mt10">
			<label class="layui-form-label">同步账号</label>
			<div class="layui-input-inline">
				{:Form::checkbox('order_from', '', config('mimo.order_from'))}
			</div>
		</div>
	</form>
</div>
<table id="dataTable" lay-filter="demoEvent"></table>

{include file="system@block/layui" /}

<script type="text/html" id="subbtnTpl">
    {{# if(d.status==0){ }} 
        <a href="{:url('status')}?id={{ d.id }}" title="审核" class="blue hisi-ajax subbtn-{{ d.id }}" lay-event="handle1" confirm="该亏损物料是否已进行处理？">待处理</a>
    {{# }else{ }}
        已处理
    {{# } }}   
</script>
<script type="text/javascript">
	function add0(m){return m<10?'0'+m:m }
	function format(shijianchuo)
	{
		//shijianchuo是整数，否则要parseInt转换
		var time = new Date(shijianchuo);
		var y = time.getFullYear();
		var m = time.getMonth()+1;
		var d = time.getDate();
		var h = time.getHours();
		var mm = time.getMinutes();
		var s = time.getSeconds();
		return y+'-'+add0(m)+'-'+add0(d);//' '+add0(h)+':'+add0(mm)+':'+add0(s)
	}
    layui.use(['table', 'tool','jquery','layer','opTable',  'tool', 'laydate', 'laytpl', 'form','upload'], function() {
        var $ = layui.jquery;
        var table = layui.table,layer=layui.layer;
		var laydate = layui.laydate;
		var laytpl = layui.laytpl;
		var form = layui.form;
		var layer = layui.layer;
        var upload = layui.upload;
		var t;
		var url="{:url('purchasingV2/index?action=refreshSearchLabels')}";
		window.setdate=function(){
			var importdates=$("#create_date_range").val().split(" ~ ")
			var startday=endday=imported_day=current_date=executImportDate=''
			startday=importdates[0];
			endday=importdates[1];
			if($(".current_import_date").val()==''){
				$(".current_import_date").val(startday);
				current_date=startday;
			}else{
				var date1 = new Date($(".current_import_date").val());
				date1=date1.getTime();
				current_date=format(date1+86400*1000);
				if(date1+86400*1000  >new Date(endday).getTime()){
					clearTimeout(t);
					return false;
				}
				$(".current_import_date").val(current_date)
			}
			executImportDate=current_date+" 00:00:00 ~ "+current_date+" 23:50:00"
			$("#sync_date_range").val(executImportDate);
			return true;
		}
		var wait_second=60;
		window.startImport=function(){
			//返回不分两次则执行setdate,  否则执行0,1,2,  

			var action=setdate();
			if(!action) return;
			importday=$(".current_import_date").val();
			var url="{:url('OrderException/importMesOrder')}"+"?action=downorder&importday="+importday;	
			var layerLoad = layer.load(0, {offset: '100px',shade: [0.5,'#F2F2F2']});
			$.ajax({
			   type: "POST",
			   url: url,
			   data: $('.sync-date-form').serialize(),
			   dataType:"json",
			   success: function(res){
				  layer.close(layerLoad);
				  var oldret= $(".importResult").html()+"<br>";
				  if (res.code == 1) {
					 $(".importResult").html(oldret+res.msg);
					 retdata=res.data;
					 if(retdata.count>0 || retdata.mighuifang>0){
					 	if(retdata.count>70){ //当天订单量大于80拆分请求
							var executImportDate=importday+" 00:00:00 ~ "+retdata.heighhour;
							$("#sync_date_range").val(executImportDate);
							requestMesOrder();
							
							setTimeout(function(){
								var executImportDate=retdata.heighhour+ " ~ "+importday+" 23:59:00";
								$("#sync_date_range").val(executImportDate);
								requestMesOrder();
							},1*20*1000);
						}else{
                            wait_second=35;
							requestMesOrder();
						}
					 }else{
					     //wait_second=20;
					 }
					 layer.msg(res.msg, {icon: 1});
				  } else {
						$(".importResult").html(oldret+res.msg);
					 layer.msg(res.msg, {icon: 2,anim: 6});
				  }
			   }
			});
			t=setTimeout(function(){
				startImport();
			},1*wait_second*1000);
		}
		window.stopImport=function(){
			clearTimeout(t);alert('success');
		}
		window.requestMesOrder=function(){
			$.ajax({
			   type: "POST",
			   url: "{:url('Order/sync')}",
			   data: $('.sync-date-form').serialize(),
			   dataType:"json",
			   success: function(res){
					var oldret= $(".importResult").html()+" --";
					$(".importResult").html(oldret+res.msg);
			   }
			});
		}
		
		
		laydate.render({
		    elem: '#create_date_range'
		    ,range: '~'
		    ,theme: 'molv'
		    ,mark: {'':''}
	    	,change: function(value, date, endDate){
		    	$('#create_date_range').val(value);
            	$('#layui-laydate1').remove();//关闭面板
            }
		});
		// 同步指定日期订单
    });
	
</script>